#include <iostream>
#include <stdlib.h>
#include "defines.h"
#include "fuzzy.h"
#include "backpro.h"
#include "analysis.h"
#include <cpptk.h>

int main()
{
    FILE * f_ptr;
    analysis pdata;

    pdata.setup_data();

    if ((f_ptr = fopen(TRAINING_FILE, "r")) == NULL)
    {
        cout << "Problem opening training data file.\n";
        exit(1);
    }

    pdata.num_data = pdata.load_data();
    cout << pdata.num_data << " training data successfuly loaded.\n";
    fclose(f_ptr);

    pdata.build_ma();
}

//int main(int argc, char * argv[])
//{
//    float error_tolerance        = 0.1;
//    float total_error            = 0.0;
//    float avg_error_per_cycle    = 0.0;
//    float error_last_cycle       = 0.0;
//    float avgerr_per_pattern     = 0.0;
//    float error_last_pattern     = 0.0;
//    float learning_parameter     = 0.02;
//    unsigned temp, startup;
//    long int vectors_in_buffer;
//    long int max_cycles;
//    long int patterns_per_cycle  = 0;
//
//    long int total_cycles, total_patterns;
//    int i;
//
//    network backp;
//
//    FILE * training_file_ptr, * weights_file_ptr, * output_file_ptr;
//    FILE * test_file_ptr, * data_file_ptr;
//
//    if ((output_file_ptr = fopen(OUTPUT_FILE, "w")) == NULL)
//    {
//        cout << "problem opening output file\n";
//        exit(1);
//    }
//
//    backp.set_training(TRAINING);
//
//    if (backp.get_training_value() == 1)
//    {
//        cout << "--> Training mode is *ON*. weights will be saved\n";
//        cout << "in the file weights.dat at the end of the\n";
//        cout << "current set of input (training) data\n";
//    }
//    else
//    {
//        cout << "--> Training mode is *OFF*. weights will be loaded\n";
//        cout << "from the file weights.dat and the current\n";
//        cout << "(test) data set will be used. For the test\n";
//        cout << "data set, the test.dat file should contain\n";
//        cout << "only inputs, and no expected outputs.\n";
//    }
//
//    if (backp.get_training_value() == 1)
//    {
//        error_tolerance = 0.1;
//        learning_parameter = 0.5;
//        max_cycles = 10;
//
//        if ((training_file_ptr = fopen(TRAINING_FILE, "r")) == NULL)
//        {
//            cout << "problem opening training file\n";
//            exit(1);
//        }
//        data_file_ptr = training_file_ptr;
//    }
//    else
//    {
//        if ((test_file_ptr = fopen(TEST_FILE, "r")) == NULL)
//        {
//            cout << "problem opening test file\n";
//            exit(1);
//        }
//        data_file_ptr = test_file_ptr;
//    }
//
//    total_cycles = 0;
//    total_patterns = 0;
//
//    backp.get_layer_info();
//    backp.set_up_network();
//
//    if (backp.get_training_value() == 1)
//    {
//        if ((weights_file_ptr = fopen(WEIGHTS_FILE, "w")) == NULL)
//        {
//            cout << "problem opening weights file\n";
//            exit(1);
//        }
//        backp.randomize_weights();
//    }
//    else
//    {
//        if ((weights_file_ptr = fopen(WEIGHTS_FILE,"r")) == NULL)
//        {
//            cout << "problem opening weights file\n";
//            exit(1);
//        }
//        backp.read_weights(weights_file_ptr);
//    }
//
//    startup = 1;
//    vectors_in_buffer = MAX_VECTORS;
//    total_error = 0;
//
//    while
//    (
//        (
//            (backp.get_training_value() == 1)
//            && (avgerr_per_pattern > error_tolerance)
//            && (total_cycles < max_cycles)
//            && (vectors_in_buffer != 0)
//        )
//        ||
//        (
//            (backp.get_training_value() == 0)
//            && (total_cycles < 1)
//        )
//        ||
//        (
//            (backp.get_training_value() == 1)
//            && (startup == 1)
//        )
//    )
//    {
//        startup = 0;
//        error_last_cycle = 0;
//        patterns_per_cycle = 0;
//
//        while ((vectors_in_buffer == MAX_VECTORS))
//        {
//            vectors_in_buffer = backp.fill_IObuffer(data_file_ptr);
//            if (vectors_in_buffer < 0)
//            {
//                cout << "error in reading in vectors, aborting\n";
//                cout << "check that there are no extra linefeeds\n";
//                cout << "in your data file, and that the number\n";
//                cout << "of layers and size of layers match the\n";
//                cout << "the parameters provided.\n";
//                exit(1);
//            }
//
//            for (i = 0; i < vectors_in_buffer; i++)
//            {
//                backp.set_up_pattern(i);
//                total_patterns++;
//                patterns_per_cycle++;
//
//                backp.forward_prop();
//
//                if (backp.get_training_value() == 0) backp.write_outputs(output_file_ptr);
//
//                if (backp.get_training_value() == 1)
//                {
//                    backp.backward_prop(error_last_pattern);
//                    error_last_cycle += error_last_pattern * error_last_pattern;
//                    backp.update_weights(learning_parameter);
//                }
//            }
//            error_last_pattern = 0;
//        }
//
//        avgerr_per_pattern = ((float) sqrt((double) error_last_cycle / patterns_per_cycle));
//        total_error += error_last_cycle;
//        total_cycles++;
//
//        cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
//        cout << total_cycles << "\t" << avgerr_per_pattern << "\n";
//
//        fseek(data_file_ptr, 0L, SEEK_SET);
//        vectors_in_buffer = MAX_VECTORS;
//    }
//
//    cout << "\n\n\n\n\n\n\n\n\n\n\n";
//    cout << "------------------------\n";
//    cout << "    done:   results in file output.dat\n";
//    cout << "            training: last vector only\n";
//    cout << "            not training: full cycle\n\n";
//
//    if (backp.get_training_value() == 1)
//    {
//        backp.write_weights(weights_file_ptr);
//        backp.write_outputs(output_file_ptr);
//        avg_error_per_cycle = (float) sqrt((double) total_error / total_cycles);
//        error_last_cycle = (float) sqrt((double) error_last_cycle);
//
//        cout << "      weights saved in file weights.dat\n";
//        cout << "\n";
//        cout << "-->average error per cycle = " << avg_error_per_cycle << " <-\n";
//        cout << "-->error last cycle= " << error_last_cycle << " <-\n";
//        cout << "->error last cycle per pattern= " << avgerr_per_pattern << " <-\n";
//    }
//
//    cout << "------>total cycles = " << total_cycles << " <--\n";
//    cout << "------>total patterns = " << total_patterns << " <---\n";
//    cout << "-------------------------\n";
//
//    fclose(data_file_ptr);
//    fclose(weights_file_ptr);
//    fclose(output_file_ptr);
//
//    return 0;
//}

//int main()
//{
//    int i = 0, j = 0, numcat = 0, randnum;
//    float l, m, h, inval = 1.0;
//    char input[30] = " ";
//    category * ptr[10];
//    float relprob[10];
//    float total = 0, runtotal = 0;
//    while (1)
//    {
//        cout << "\nPlease type in a category name, e.g. Cool\n";
//        cout << "Enter one word without spaces\n";
//        cout << "When you are done, type `done' :\n\n";
//        ptr[i] = new category;
//        cin >> input;
//        if ((input[0] == 'd' && input[1] == 'o' && input[2] == 'n' && input[3] == 'e')) break;
//        ptr[i]->setname(input);
//        cout << "\nType in the lowval, midval and highval\n";
//        cout << "for each category, separated by spaces\n";
//        cout << " e.g. 1.0 3.0 5.0 :\n\n";
//        cin >> l >> m >> h;
//        ptr[i]->setval(h,m,l);
//        i++;
//    }
//    numcat=i;
//
//    cout <<"\n\n";
//    cout << "===================================\n";
//    cout << "==Fuzzifier is ready for data==\n";
//    cout << "===================================\n";
//    while (1)
//    {
//        cout << "\ninput a data value, type 0 to terminate\n";
//        cin >> inval;
//        if (inval == 0) break;
//        total=0;
//        for (j=0;j<numcat;j++)
//        {
//            relprob[j]=100*ptr[j]->getshare(inval);
//            total+=relprob[j];
//        }
//        if (total==0)
//        {
//            cout << "data out of range\n";
//            exit(1);
//        }
//        randnum=randomnum((int)total);
//        j=0;
//        runtotal=relprob[0];
//        while ((runtotal<randnum)&&(j<numcat))
//        {
//            j++;
//            runtotal += relprob[j];
//        }
//        cout << "\nOutput fuzzy category is ==> " <<
//        ptr[j]->getname()<<"<== \n";
//        cout <<"category\t"<<"membership\n";
//        cout <<"---------------\n";
//        for (j=0;j<numcat;j++)
//        {
//            cout << ptr[j]->getname()<<"\t\t"<< (relprob[j]/total) <<"\n";
//        }
//    }
//    cout << "\n\nAll done. Have a fuzzy day !\n";
//}

